package com.foreveross.push.demo;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

import com.foreveross.chameleon.config.Preferences;
import com.foreveross.zillasdk.Zilla;
import com.foreveross.zillasdk.ZillaDelegate;

import org.apache.cordova.CordovaActivity;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;

public class MainActivity extends CordovaActivity {

    //PushPlugin mPush = new PushPlugin();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        getToken();
        loadUrl("file:///android_asset/www/push.html");
        initJsCall();


    }


    private void getToken() {
       String token = Preferences.getInstance(this).getToken();
       if("".equals(token)) {
            authentication();
        } else {
            Log.e("MainActivity","Token has exist!");
          // mPush.register();
       }
    }

    public void initJsCall() {
        appView.getSettings().setJavaScriptEnabled(true);
        appView.addJavascriptInterface(new NotificationPushMsg(this, appView), "notificationPushMsg");
    }

    //向服务器鉴权
    private void authentication() {
        final Context context = MainActivity.this;
        ZillaDelegate delegate = new ZillaDelegate() {

            @Override
            public void requestSuccess(String json) {
                try {
                    if(json.equals("403")) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle("提示");
                        builder.setMessage("应用验证失败");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }
                        );
                        Dialog dialog = builder.create();
                        dialog.show();
                    } else if(json.equals("400")) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(context);
                        builder.setTitle("提示");
                        builder.setMessage("应用已删除");
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                }
                        );
                        Dialog dialog = builder.create();
                        dialog.show();
                    } else {
                        JSONObject jb = new JSONObject(json);
                        String token = jb.getString("token");
                        String expired = jb.getString("expired");
                        // 保存token和expired
                        Preferences.saveToken(token, expired);
                       Log.e("MainActivity","save token and expired");
                       // mPush.register();
                    }

                } catch(JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void requestStart() {
                // TODO Auto-generated method stub

            }

            @Override
            public void requestFailed(String errorMessage) {
                Toast.makeText(MainActivity.this, "应用验证失败，请检查网络", Toast.LENGTH_SHORT).show();
                finish();
            }
        };
        Zilla.getZilla().appAuth(context, delegate);

    }
}
